/**
 * 
 */
package com.probiz.estore.eai.common;

import java.io.File;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

/**
 * EAI 工具类
 * 
 * 与EAI相关的且与业务无关的公共的操作可以放到此类中
 * 
 * @author pengzhirong
 *
 */
public class EaiUtil {
	protected final transient Log	logger	= LogFactory.getLog(getClass());
	
	/**
	 * 业务实体集合类的后缀, 如SaleorderSet为业务实体Saleorder的集合类，后缀为Set
	 */
	private final static String ENTITY_SET_CLASS_NAME_SUFFIX = "Set";
	
	private final static EaiUtil	eaiUtil				= new EaiUtil();
	
	private ReloadableResourceBundleMessageSource eaiConfig = null;
	
	public void setEaiConfig(ReloadableResourceBundleMessageSource eaiConfig) {
		this.eaiConfig = eaiConfig;
	}

	/**
	 * 获取eai.properties中configKey对应的value
	 * key-value
	 * @param configKey
	 * @return
	 */
	public String getConfig(String configKey){
		return eaiConfig.getMessage(configKey, null, null);
	}
	
	public static EaiUtil getInstance() {
		return eaiUtil;
	}
	
	/**
	 * 通过实体类得到相关的XML文件的路径配置
	 * 如Saleorder得到eai.properties文件中key为saleorder.importXmlFile的value值
	 * 
	 * @param entityClass
	 * @return
	 */
	public String getTargetXmlFilePathByEntityClass(Class entityClass, boolean isMarshall){
		//得到eai.properties文件中相关的路径配置
		String entityClassName = getClassName(entityClass);
		String targetXmlFilePathKey = null;
		if(isMarshall){
			targetXmlFilePathKey = entityClassName + ".exportXmlFile";
		}else{
			targetXmlFilePathKey = entityClassName + ".importXmlFile";
		}
		
		String targetXmlFilePath = eaiUtil.getConfig(targetXmlFilePathKey);
		if(logger.isDebugEnabled())
			logger.debug("TargetXmlFilePath key-value:"+targetXmlFilePathKey+"-"+targetXmlFilePath);
		
		return targetXmlFilePath;
	}
	
	/**
	 * 通过实体集合类得到相关的XML文件的路径配置
	 * 如SaleorderSet得到eai.properties文件中key为saleorder.importXmlFile的value值
	 * 
	 * @param entityClass
	 * @return
	 */
	public String getTargetXmlFilePathByEntitySetClass(Class entitySetClass, boolean isMarshall){
		//获取路径配置的key
		String entitySetClassName = getClassName(entitySetClass);
		int index  = entitySetClassName.lastIndexOf(ENTITY_SET_CLASS_NAME_SUFFIX);
		String entityClassName = entitySetClassName;
		//必须以ENTITY_SET_CLASS_NAME_SUFFIX变量值结尾
		if(entitySetClassName.endsWith(ENTITY_SET_CLASS_NAME_SUFFIX))
			entityClassName = entitySetClassName.substring(0, index);
		
		String targetXmlFilePathKey = null;
		if(isMarshall){
			targetXmlFilePathKey = entityClassName + ".exportXmlFile";
		}else{
			targetXmlFilePathKey = entityClassName + ".importXmlFile";
		}
		
		String targetXmlFilePath = eaiUtil.getConfig(targetXmlFilePathKey);
		if(logger.isDebugEnabled())
			logger.debug("TargetXmlFilePath key-value:"+targetXmlFilePathKey+"-"+targetXmlFilePath);
		
		return targetXmlFilePath;
			
	}
	
	/**
	 * 得到类的名称
	 * 如类Saleorder，则得到类名saleorder
	 * @param clazz
	 * @return
	 */
	public String getClassName(Class clazz){
		return clazz.getSimpleName().substring(0, 1).toLowerCase() + clazz.getSimpleName().substring(1);
	}
	
	/**
	 * 得到最新的要处理的xml文件全路径
	 * @param targetXmlFilePath  配置的xml文件全路径
	 * @return
	 */
	public String getLatestTargetXmlFilePath(String targetXmlFilePath){
		
		int index = targetXmlFilePath.lastIndexOf("/");
		File xmlFileDirectory = new File(targetXmlFilePath.substring(0, index));
		
		String fileNamePrefix = targetXmlFilePath.substring(index+1, targetXmlFilePath.lastIndexOf("."));
		File[] xmlFileList = xmlFileDirectory.listFiles(new XmlFileFilter(fileNamePrefix));
		
		File latestXmlFile = null;
		if(xmlFileList!=null && xmlFileList.length>0){
			latestXmlFile = xmlFileList[0];
			if(xmlFileList.length>1){
				for(int i=1; i<xmlFileList.length; i++){
					int temp = xmlFileList[i].getName().compareToIgnoreCase(latestXmlFile.getName());
					if(temp>0)
						latestXmlFile = xmlFileList[i];
				}
			}
		}
		if(latestXmlFile!=null){
			return latestXmlFile.getAbsolutePath();
		}
			
		return targetXmlFilePath;
	}
}
